/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package codegen;

/**
 *
 * @author Felipe Gutierrez
 */


import java.util.ArrayList;

import net.java.dev.greenbox.base.Application;
import net.java.dev.greenbox.base.Attribute;
import net.java.dev.greenbox.base.Clazz;
import net.java.dev.greenbox.base.TagValue;

import xmibase.XmiAttribute;
import xmibase.XmiClass;
import xmibase.XmiInterface;
import xmibase.XmiPackage;
import xmibase.XmiTagValue;

public class Conversor {

	private ArrayList<Clazz> greenBoxClasses;
	private Application application;

	public Conversor(ArrayList<Clazz> greenBoxClazzes) {
		this.greenBoxClasses = greenBoxClazzes;
		this.application = new Application();

	}

	public void execute(XmiPackage xmiPKG) {

		xmiClassListToClazzList(xmiPKG.getClasses());
		setSuperClazz();
		this.application.setClasses(this.greenBoxClasses);
		this.application.setName(xmiPKG.getName());

	}




	private void setSuperClazz() {
		for (Clazz tmpClazz : this.greenBoxClasses) {
			if (tmpClazz.getHasSuperClass()) {
				for (Clazz tmpSuperClazz : this.greenBoxClasses) {
					if (tmpClazz.getSuperClazz().getId().equals(
							tmpSuperClazz.getId())) {
						tmpClazz.setSuperClazz(tmpSuperClazz);
					}
				}
			}
		}
	}

	public void xmiClassListToClazzList(ArrayList<XmiClass> xmiClasses) {
		for (XmiClass tmpXMIClass : xmiClasses) {

			Clazz tmpClazz = new Clazz();
			tmpClazz.setId(tmpXMIClass.getId());
			tmpClazz.setClassName(tmpXMIClass.getName());
			tmpClazz.setPackageName(tmpXMIClass.getPackageName());
			tmpClazz.setHasSuperClass(tmpXMIClass.getHasSuperClass());
			tmpClazz.setHasInterfaceClass(tmpXMIClass.hasInterface());

			tmpClazz.setAbstract(tmpXMIClass.isAbstract());
			tmpClazz.setSpecification(tmpXMIClass.isSpecification());
			tmpClazz.setLeaf(tmpXMIClass.isLeaf());
			tmpClazz.setRoot(tmpXMIClass.isRoot());
			tmpClazz.setActive(tmpXMIClass.isActive());
			//convert XMITagValue to GreenTagValue
			for (XmiTagValue xmiTag : tmpXMIClass.getTagsValue()) {
				TagValue tag = new TagValue();
				tag.setId(xmiTag.getName());
				tag.setName(xmiTag.getName());
				tag.setValue(xmiTag.getTagValue());

				tmpClazz.addTagValue(tag);
			}
			if (tmpClazz.getHasSuperClass()) {
				Clazz tmpSuperClazz = new Clazz();
				tmpSuperClazz.setId(tmpXMIClass.getSuperClass().getId());
				tmpClazz.setSuperClazz(tmpSuperClazz);

			}
			if ( tmpClazz.hasInterfaceClass()){

				for ( XmiInterface tmpXMIInterface : tmpXMIClass.getInterfaces()) {
					Clazz tmpInterfaceClazz = new Clazz();

					tmpInterfaceClazz.setClassName(tmpXMIInterface.getName());
					tmpClazz.addInterface(tmpInterfaceClazz);
				}
			}
			converteXMIAttToGreenAtt(tmpXMIClass.getAttributes(), tmpClazz, false);
			converteXMIAttToGreenAtt(tmpXMIClass.getAssocitesAttributes(), tmpClazz, true);
			this.greenBoxClasses.add(tmpClazz);
		}
	}

	private void converteXMIAttToGreenAtt(ArrayList<XmiAttribute> xmiAtts, Clazz tmpClazz,
			boolean isAssociateAttribute) {

		for (XmiAttribute tmpXMIAtt : xmiAtts) {

			Attribute tmpGreenAtt = new Attribute();
			// set attributes at GreenBox Attribute
			tmpGreenAtt.setDataType(tmpXMIAtt.getDataType());
			tmpGreenAtt.setName(tmpXMIAtt.getName());
			tmpGreenAtt.setChangeability(tmpXMIAtt.getChangeability());
			tmpGreenAtt.setSpecification(tmpXMIAtt.isSpecification());
			tmpGreenAtt.setOwnerScope(tmpXMIAtt.getOwnerScope());
			tmpGreenAtt.setVisibility(tmpXMIAtt.getVisibility());
			tmpGreenAtt.setPackageType(tmpXMIAtt.getPackageType());
			tmpGreenAtt.setUpperMultiplicity(tmpXMIAtt.getUpperMultiplicity());
			tmpGreenAtt.setLowerMultiplicity(tmpXMIAtt.getLowerMultiplicity());
			for (XmiTagValue xmiTag : tmpXMIAtt.getTagsValue()) {
				TagValue tag = new TagValue();
				tag.setId(xmiTag.getName());
				tag.setName(xmiTag.getName());
				tag.setValue(xmiTag.getTagValue());

				tmpGreenAtt.addTagValue(tag);
			}

			if (isAssociateAttribute) {
				tmpGreenAtt.setAggregation(tmpXMIAtt.getAggregation());
				tmpClazz.addAssociateAttribute(tmpGreenAtt);
			} else {
				tmpClazz.addAttribute(tmpGreenAtt);
			}
		}

	}

	public ArrayList<Clazz> getGreenBoxClasses() {
		return greenBoxClasses;
	}

	public void setGreenBoxClasses(ArrayList<Clazz> greenBoxClasses) {
		this.greenBoxClasses = greenBoxClasses;
	}
	public Application getApplication() {
		return application;
	}

	public void setApplication(Application application) {
		this.application = application;
	}
}
